//
//  YHMainTabBar.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/19.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHMainTabBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(addBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //在tabbar 添加一个 '+'按钮
    //1. 将所有的tabbarButton 宽度缩小
    //2. 将中间的位置 空出来
    //3. 添加自定义按钮
    lazy var addBtn: UIButton = {
    
       let btn = UIButton()
        
        //设置图片
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        return btn
    
    }()

    
    
    //布局子视图
    //获取一条分割线的正确方式 1 / UIScreen.mainScreen().scale
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //获取每一个按钮的宽度和高度
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //遍历子视图 设置frame
        var index: CGFloat = 0
        
        for subView in subviews {
            //判断是否是 UITabBarButton(私有类) 类型下的控件
            if subView .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
//                print(subView)
                subView.frame = CGRectOffset(rect, w * index, 0)
                
                //判断遍历到第几个 控件
//                if index == 1 {
//                    
//                    index += 1
//                }
                //三目简写
                index += (index == 1 ? 2 : 1)
                
            }
            addBtn.frame = CGRectOffset(rect, 2 * w, 0)
//            bringSubviewToFront(addBtn)
            
        }
        
        
    }
    

}
